package com.movieproject.daos.imp;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.movieproject.daos.UserDAO;
import com.movieproject.dataobjects.User;

@Service
public class UserDAOImp implements UserDAO {

	private DataSource dataSource;
	
	@Autowired
	private SessionFactory sessionFactory;

	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;
	}

	public void insert(User user) {
		String sql = "INSERT INTO USERS" + "(_y4, _x4, AccessLevel, FirstName, LastName, Email, Address, City, State, Zip) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?,?)";

		Connection conn = null;

		try {
			conn = dataSource.getConnection();
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setString(1, user.getUsername());
			ps.setString(2, user.getPassword());
			ps.setInt(3, 1);
			ps.setString(4, user.getFirstName());
			ps.setString(5, user.getLastName());
			ps.setString(6, user.getEmail());
			ps.setString(7, user.getAddress());
			ps.setString(8, user.getCity());
			ps.setString(9, user.getState());
			ps.setString(10, user.getZip());
			ps.executeUpdate();
			ps.close();

		} catch (SQLException e) {
			throw new RuntimeException(e);

		} finally {
			if (conn != null) {
				try {
					conn.close();
				} catch (SQLException e) {}
			}
		}
	}


	@Override
	public List<User> getAllUsers() {
		Query query = sessionFactory.getCurrentSession().createQuery("from User");
		List<?> l = query.list(); // query returns a raw list. 
		ArrayList<User> userList = new ArrayList<User>();
		for(Object o : l) {	// objects in list must be cast individually
			userList.add((User) o);
		}
		return userList;
	}

	/**
	 * Get's the user by the user of a userID number
	 */
	@Override
	public User getUser(int userID) {
		return (User) sessionFactory.getCurrentSession().get(User.class, userID);
	}

/*	@Override
	public User getUser(String userName) {
		return (User) sessionFactory.getCurrentSession().get(User.class, userName);
	}*/

	@Override
	public void updateUser(User user) {
		sessionFactory.getCurrentSession().update(user);
	}
	


	@Override
	public void deleteUser(User user) {
		sessionFactory.getCurrentSession().delete(user);
	}

	/*@Override
	public User getUser(String userName) {
		String username = null;
		try {
			username = AppSecurity.createHash(userName);
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
			return null;
		} catch (InvalidKeySpecException e) {
			e.printStackTrace();
			return null;
		}
		
		Query query = sessionFactory.getCurrentSession().createQuery("from User where username = :u");
		query.setParameter("u", username);
		query.setMaxResults(1);
		List<?> list = query.list();
		User user = null;
		try {
			 user = (User)list.get(0);
		}
		catch(ArrayIndexOutOfBoundsException e) {
			return user;
		}
		catch(IndexOutOfBoundsException e) {
			return user;
		}
		
		return user;
	}*/

	/**
	 * Get's the user by the user of the email string
	 */
	@Override
	public User getUser(String email) {
		Query query = sessionFactory.getCurrentSession().createQuery("from User where Email = :email");
		query.setParameter("email", email);
		query.setMaxResults(1);
		List<?> list = query.list();
		if(list.size() <= 0) {
			return null;
		}
		
		return (User)list.get(0);
	}
}
